﻿Public Structure AnimationPlayer
	'mencari animasi mana yang sedang berjalan

	Public ReadOnly Property Animation As Animation
		Get
			Return _animation
		End Get
	End Property
	Private _animation As Animation

	'mencari index dari frame yang sedang berjalan dalam animasi
	ReadOnly Property FrameIndex As Integer
		Get
			Return _frameIndex
		End Get
	End Property
	Private _frameIndex As Integer

	'jumlah waktu dalam detik yang ditampilkan oleh current frame
	Private _time As Single

	'mencari titik origin dari texture yang terletak di tengah tiap frame
	Public ReadOnly Property Origin As Vector2
		Get
			Return New Vector2(Animation.FrameWidth / 2.0F, Animation.FrameHeight)

		End Get
	End Property

	'memulai atau melanjutkan kembali jalannya animasi
	Public Sub PlayAnimation(Anim As Animation)
		'jika animasi telah berjalan,tidak perlu di restart
		If ReferenceEquals(_animation, Anim) Then Exit Sub

		'memulai animasi baru
		Me._animation = Animation
		Me._frameIndex = 0
		Me._time = 0.0F
	End Sub

	'Advances the time position and draws the current frame of the animation.
	Public Sub Draw(GameTime As GameTime, SpriteBatch As SpriteBatch, position As Vector2, SpriteEffects As SpriteEffects)
		If (Animation Is Nothing) Then Throw New NotSupportedException("ga ada animasi yang jalan")
		'proses jalannya waktu
		_time += CSng(GameTime.ElapsedGameTime.TotalSeconds)
		While (_time > Animation.FrameTime)
			_time -= Animation.FrameTime

			'Advance the frame index looping or clamping as appropriate.
			If (Animation.IsLooping) Then
				_frameIndex = (FrameIndex + 1) Mod Animation.FrameCount

			Else
				_frameIndex = Math.Min(FrameIndex + 1, Animation.FrameCount - 1)
			End If
		End While

		' Calculate the source rectangle of the current frame.
		Dim source As Rectangle = New Rectangle(FrameIndex * Animation.Texture.Height, 0, Animation.Texture.Height, Animation.Texture.Height)

		'Draw the current frame
		SpriteBatch.Draw(Animation.Texture, position, source, Color.White, 0.0F, Origin, 1.0F, SpriteEffects, 0.0F)
	End Sub

End Structure
